<?php
// this doSQL script is called from the addedit.php script
// its purpose is to use the CEinstein class to interoperate with the database (store, edit, delete)

/* the following variables can be retreived via POST from einstein/addedit.php:
** int einstein_id	is '0' if a new database object has to be stored or the id of an existing quote that should be overwritten or deleted in the db
** str einstein_quote	the text of the quote that should be stored
** int del		bool flag, in case of presence the row with the given einstein_id has to be dropped from db
*/

// create a new instance of the einstein class
$obj = new CQuotations();
$uid = $AppUI->user_id;
$msg = '';	// reset the message string

// detect if a delete operation has to be processed
$updateStatus = dPgetParam($_POST, 'updateStatus', 0);
/**
 * TODO:
 * 	Ajax Completer
 */
//$loadStatus = dPgetParam($_POST, 'ajaxLoad', $_GET['ajaxLoad']);
//if($loadStatus){
//	$format = dPgetParam($_POST, 'format', $_GET['format']);
//	$keyword = dPgetParam($_POST, 'keyword', $_GET['keyword']);
//	if(''!=$keyword){
//		$extra = array();
//		$extra['where'] = 'quote_name like \'%'. $keyword . '%\'';
//		$result = $obj->getLoadList($uid, 'quote_id as id, quote_code as code, quote_name as name', '',null, $extra);
//		// Add link to link
//		for($i = 0; $i < count($result); $i++){
//			$result[$i]['link'] = 'index.php?m=quotations&a=quote_view&quote_id='.$result[$i]['id'];
//		}
//		//if(extension_loaded('json')){
//			echo json_encode($result);
//		//}
//	}
//	echo '';exit;
//}

if($updateStatus != '0'){	
	$idArr = $_POST['quoteIdArr'];
	$statusId = $_POST['quote_status'];

//	$confirmed = dPgetSysVal('QuotationConfirmed');
//
//	// Get code of status
//	// Load Object Status
//	require_once($AppUI->getMasterDataClass('mdstatus'));
//	$objMD = new CMdStatus();
//	$objMD->load($statusId);
//	$data = array();
//	$completed = intval($confirmed['code']==$objMD->status_code);
	if(!is_null($idArr) and (sizeof($idArr) > 0)){
		for($i = 0; $i< sizeof($idArr); $i++){
			if(0 < intval($idArr[$i])){
				$obj = new CQuotations();
				$obj->quote_id = $idArr[$i];
				$obj->quote_status = $statusId;
				$obj->store();
			}
		}
	}
	$AppUI->setMsg('Quotations status updated', UI_MSG_OK, true );
	$AppUI->redirect();
}
?>
